{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5a6e64cf-6093-4dcf-b0be-9eb242123a71",
   "metadata": {},
   "source": [
    "### retrieval"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cab719b4-c775-4eb6-ae1c-2633da64706a",
   "metadata": {},
   "source": [
    "- embedding-based\n",
    "    - BGE: BAAI General Embedding\n",
    "        - BERT + 对抗样本训练\n",
    "            - 锚点样本 ($p$)\n",
    "            - 普通正样本，硬正样本 ($q$)\n",
    "            - 普通负样本，硬负样本 ($q'$)\n",
    "- term-based\n",
    "    - bm25：tf(词频)-idf(稀有性，log)\n",
    "        - 倒排索引（inverted index）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "249bfbe6-7d8d-43d9-876f-1133e55638ec",
   "metadata": {},
   "source": [
    "- info-nce loss\n",
    "    - -log(`[0-1]`): 这是个数学技巧。我们想让括号里的值越大越好（越接近1）\n",
    "$$\n",
    "\\min \\sum_{(p, q)} - \\log \\frac{\\exp(\\langle \\mathbf{e}_p, \\mathbf{e}_q \\rangle / \\tau)}{\\exp(\\langle \\mathbf{e}_p, \\mathbf{e}_q \\rangle / \\tau) + \\sum_{Q'} \\exp(\\langle \\mathbf{e}_p, \\mathbf{e}_{q'} \\rangle / \\tau)}\n",
    "$$\n",
    "\n",
    "- 硬负样本挖掘 (Hard Negative Mining), 其中 $\\mathcal{H}_p$ 是从所有负样本中挑选出来的硬负样本集合。\n",
    "    - 硬正样本挖掘 (Hard Positive Mining): 这通常不是通过修改损失函数本身，而是通过数据增强策略来实现的。在为锚点 p 生成其正样本 q 时，我们刻意使用非常强的增强手段，使得 p 和 q 在像素层面差异巨大，从而创造出一个“硬正样本”。损失函数的形式保持不变，但输入的样本对本身已经具备了“硬”的属性。\n",
    "$$\n",
    "\\min \\sum_{(p, q)} - \\log \\frac{\\exp(\\langle \\mathbf{e}_p, \\mathbf{e}_q \\rangle / \\tau)}{\\exp(\\langle \\mathbf{e}_p, \\mathbf{e}_q \\rangle / \\tau) + \\sum_{\\mathbf{e}_{q'} \\in \\mathcal{H}_p} \\exp(\\langle \\mathbf{e}_p, \\mathbf{e}_{q'} \\rangle / \\tau)}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "213d882a-4961-4713-9a30-0d92bfd4416a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
